/*   1:    */ package org.jetbrains.generate.tostring.psi;
/*   2:    */ 
/*   3:    */

import com.intellij.openapi.project.Project;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.PropertyUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.generate.tostring.util.StringUtil;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */
/*  26:    */
/*  27:    */
/*  28:    */
/*  29:    */
/*  30:    */
/*  31:    */
/*  32:    */ 
/*  33:    */ public class PsiAdapter
/*  34:    */ {
/*  35:    */   public static boolean isConstantField(PsiField paramPsiField)
/*  36:    */   {
/*  37: 51 */     PsiModifierList localPsiModifierList = paramPsiField.getModifierList();
/*  38: 52 */     if (localPsiModifierList == null) {
/*  39: 53 */       return false;
/*  40:    */     }
/*  41: 57 */     if (!localPsiModifierList.hasModifierProperty("static")) {
/*  42: 58 */       return false;
/*  43:    */     }
/*  44: 62 */     return !StringUtil.hasLowerCaseChar(paramPsiField.getName());
/*  45:    */   }
/*  46:    */   
/*  47:    */   @Nullable
/*  48:    */   public static PsiMethod findMethodByName(PsiClass paramPsiClass, String paramString)
/*  49:    */   {
/*  50: 75 */     PsiMethod[] arrayOfPsiMethod = paramPsiClass.getMethods();
/*  51: 78 */     for (int i = arrayOfPsiMethod.length - 1; i >= 0; i--)
/*  52:    */     {
/*  53: 79 */       PsiMethod localPsiMethod = arrayOfPsiMethod[i];
/*  54: 80 */       if (paramString.equals(localPsiMethod.getName())) {
/*  55: 81 */         return localPsiMethod;
/*  56:    */       }
/*  57:    */     }
/*  58: 84 */     return null;
/*  59:    */   }
/*  60:    */   
/*  61:    */   public static boolean isPrimitiveArrayType(PsiType paramPsiType)
/*  62:    */   {
/*  63: 94 */     return ((paramPsiType instanceof PsiArrayType)) && (isPrimitiveType(((PsiArrayType)paramPsiType).getComponentType()));
/*  64:    */   }
/*  65:    */   
/*  66:    */   public static boolean isObjectArrayType(PsiType paramPsiType)
/*  67:    */   {
/*  68:104 */     return ((paramPsiType instanceof PsiArrayType)) && (!isPrimitiveType(((PsiArrayType)paramPsiType).getComponentType()));
/*  69:    */   }
/*  70:    */   
/*  71:    */   public static boolean isStringArrayType(PsiType paramPsiType)
/*  72:    */   {
/*  73:114 */     if (isPrimitiveType(paramPsiType)) {
/*  74:115 */       return false;
/*  75:    */     }
/*  76:117 */     return paramPsiType.getCanonicalText().indexOf("String[]") > 0;
/*  77:    */   }
/*  78:    */   
/*  79:    */   public static boolean isCollectionType(PsiElementFactory paramPsiElementFactory, PsiType paramPsiType)
/*  80:    */   {
/*  81:128 */     return isTypeOf(paramPsiElementFactory, paramPsiType, "java.util.Collection");
/*  82:    */   }
/*  83:    */   
/*  84:    */   public static boolean isMapType(PsiElementFactory paramPsiElementFactory, PsiType paramPsiType)
/*  85:    */   {
/*  86:139 */     return isTypeOf(paramPsiElementFactory, paramPsiType, "java.util.Map");
/*  87:    */   }
/*  88:    */   
/*  89:    */   public static boolean isSetType(PsiElementFactory paramPsiElementFactory, PsiType paramPsiType)
/*  90:    */   {
/*  91:150 */     return isTypeOf(paramPsiElementFactory, paramPsiType, "java.util.Set");
/*  92:    */   }
/*  93:    */   
/*  94:    */   public static boolean isListType(PsiElementFactory paramPsiElementFactory, PsiType paramPsiType)
/*  95:    */   {
/*  96:161 */     return isTypeOf(paramPsiElementFactory, paramPsiType, "java.util.List");
/*  97:    */   }
/*  98:    */   
/*  99:    */   public static boolean isStringType(PsiElementFactory paramPsiElementFactory, PsiType paramPsiType)
/* 100:    */   {
/* 101:172 */     return isTypeOf(paramPsiElementFactory, paramPsiType, "java.lang.String");
/* 102:    */   }
/* 103:    */   
/* 104:    */   public static boolean isObjectType(PsiElementFactory paramPsiElementFactory, PsiType paramPsiType)
/* 105:    */   {
/* 106:183 */     return isTypeOf(paramPsiElementFactory, paramPsiType, "java.lang.Object");
/* 107:    */   }
/* 108:    */   
/* 109:    */   public static boolean isDateType(PsiElementFactory paramPsiElementFactory, PsiType paramPsiType)
/* 110:    */   {
/* 111:194 */     return isTypeOf(paramPsiElementFactory, paramPsiType, "java.util.Date");
/* 112:    */   }
/* 113:    */   
/* 114:    */   public static boolean isCalendarType(PsiElementFactory paramPsiElementFactory, PsiType paramPsiType)
/* 115:    */   {
/* 116:205 */     return isTypeOf(paramPsiElementFactory, paramPsiType, "java.util.Calendar");
/* 117:    */   }
/* 118:    */   
/* 119:    */   public static boolean isBooleanType(PsiElementFactory paramPsiElementFactory, PsiType paramPsiType)
/* 120:    */   {
/* 121:216 */     if (isPrimitiveType(paramPsiType))
/* 122:    */     {
/* 123:218 */       String str = paramPsiType.getCanonicalText();
/* 124:219 */       return "boolean".equals(str);
/* 125:    */     }
/* 126:222 */     return isTypeOf(paramPsiElementFactory, paramPsiType, "java.lang.Boolean");
/* 127:    */   }
/* 128:    */   
/* 129:    */   public static boolean isNumericType(PsiElementFactory paramPsiElementFactory, PsiType paramPsiType)
/* 130:    */   {
/* 131:234 */     if (isPrimitiveType(paramPsiType))
/* 132:    */     {
/* 133:236 */       String str = paramPsiType.getCanonicalText();
/* 134:237 */       return ("byte".equals(str)) || ("double".equals(str)) || ("float".equals(str)) || ("int".equals(str)) || ("long".equals(str)) || ("short".equals(str));
/* 135:    */     }
/* 136:240 */     return isTypeOf(paramPsiElementFactory, paramPsiType, "java.lang.Number");
/* 137:    */   }
/* 138:    */   
/* 139:    */   public static boolean hasImportStatement(PsiJavaFile paramPsiJavaFile, String paramString)
/* 140:    */   {
/* 141:252 */     PsiImportList localPsiImportList = paramPsiJavaFile.getImportList();
/* 142:253 */     if (localPsiImportList == null) {
/* 143:254 */       return false;
/* 144:    */     }
/* 145:257 */     if (paramString.endsWith(".*")) {
/* 146:258 */       return localPsiImportList.findOnDemandImportStatement(a(paramString)) != null;
/* 147:    */     }
/* 148:260 */     return localPsiImportList.findSingleClassImportStatement(paramString) != null;
/* 149:    */   }
/* 150:    */   
/* 151:    */   public static void addImportStatement(PsiJavaFile paramPsiJavaFile, String paramString)
/* 152:    */   {
/* 153:274 */     PsiElementFactory localPsiElementFactory = JavaPsiFacade.getInstance(paramPsiJavaFile.getProject()).getElementFactory();
/* 154:275 */     PsiImportStatement localPsiImportStatement = localPsiElementFactory.createImportStatementOnDemand(a(paramString));
/* 155:    */     
/* 156:    */ 
/* 157:278 */     PsiImportList localPsiImportList = paramPsiJavaFile.getImportList();
/* 158:279 */     if (localPsiImportList != null) {
/* 159:280 */       localPsiImportList.add(localPsiImportStatement);
/* 160:    */     }
/* 161:283 */     JavaCodeStyleManager.getInstance(paramPsiJavaFile.getProject()).optimizeImports(paramPsiJavaFile);
/* 162:    */   }
/* 163:    */   
/* 164:    */   private static String a(String paramString)
/* 165:    */   {
/* 166:297 */     if (paramString.endsWith(".*")) {
/* 167:298 */       return paramString.substring(0, paramString.length() - 2);
/* 168:    */     }
/* 169:300 */     boolean bool = StringUtil.hasUpperCaseChar(paramString);
/* 170:302 */     if (bool)
/* 171:    */     {
/* 172:304 */       int i = paramString.lastIndexOf(".");
/* 173:305 */       return paramString.substring(0, i);
/* 174:    */     }
/* 175:308 */     return paramString;
/* 176:    */   }
/* 177:    */   
/* 178:    */   @Nullable
/* 179:    */   public static String getTypeQualifiedClassName(PsiType paramPsiType)
/* 180:    */   {
/* 181:322 */     if (isPrimitiveType(paramPsiType)) {
/* 182:323 */       return null;
/* 183:    */     }
/* 184:327 */     String str = paramPsiType.getCanonicalText();
/* 185:328 */     if (str.endsWith("[]")) {
/* 186:329 */       return str.substring(0, str.length() - 2);
/* 187:    */     }
/* 188:332 */     return str;
/* 189:    */   }
/* 190:    */   
/* 191:    */   @Nullable
/* 192:    */   public static String getTypeClassName(PsiType paramPsiType)
/* 193:    */   {
/* 194:344 */     String str = getTypeQualifiedClassName(paramPsiType);
/* 195:347 */     if (str == null) {
/* 196:348 */       return null;
/* 197:    */     }
/* 198:351 */     int i = str.lastIndexOf('.');
/* 199:352 */     return str.substring(i + 1, str.length());
/* 200:    */   }
/* 201:    */   
/* 202:    */   @Nullable
/* 203:    */   public static PsiMethod findPublicStaticVoidMainMethod(PsiClass paramPsiClass)
/* 204:    */   {
/* 205:363 */     PsiMethod[] arrayOfPsiMethod1 = paramPsiClass.findMethodsByName("main", false);
/* 206:366 */     for (PsiMethod localPsiMethod : arrayOfPsiMethod1) {
/* 207:368 */       if (localPsiMethod.hasModifierProperty("public")) {
/* 208:373 */         if (localPsiMethod.hasModifierProperty("static"))
/* 209:    */         {
/* 210:378 */           PsiType localPsiType = localPsiMethod.getReturnType();
/* 211:379 */           if (PsiType.VOID.equals(localPsiType))
/* 212:    */           {
/* 213:384 */             PsiParameter[] arrayOfPsiParameter = localPsiMethod.getParameterList().getParameters();
/* 214:385 */             if (arrayOfPsiParameter.length == 1) {
/* 215:390 */               if (isStringArrayType(arrayOfPsiParameter[0].getType())) {
/* 216:395 */                 return localPsiMethod;
/* 217:    */               }
/* 218:    */             }
/* 219:    */           }
/* 220:    */         }
/* 221:    */       }
/* 222:    */     }
/* 223:399 */     return null;
/* 224:    */   }
/* 225:    */   
/* 226:    */   @Nullable
/* 227:    */   public static PsiComment addOrReplaceJavadoc(PsiMethod paramPsiMethod, String paramString, boolean paramBoolean)
/* 228:    */   {
/* 229:413 */     Project localProject = paramPsiMethod.getProject();
/* 230:414 */     PsiElementFactory localPsiElementFactory = JavaPsiFacade.getInstance(localProject).getElementFactory();
/* 231:415 */     PsiComment localPsiComment = localPsiElementFactory.createCommentFromText(paramString, null);
/* 232:    */     
/* 233:    */ 
/* 234:418 */     PsiDocComment localPsiDocComment = paramPsiMethod.getDocComment();
/* 235:419 */     if (localPsiDocComment != null)
/* 236:    */     {
/* 237:420 */       if (paramBoolean)
/* 238:    */       {
/* 239:422 */         localPsiDocComment.replace(localPsiComment);
/* 240:423 */         localCodeStyleManager = CodeStyleManager.getInstance(localProject);
/* 241:424 */         localCodeStyleManager.reformat(paramPsiMethod);
/* 242:425 */         return localPsiComment;
/* 243:    */       }
/* 244:428 */       return null;
/* 245:    */     }
/* 246:432 */     paramPsiMethod.addBefore(localPsiComment, paramPsiMethod.getFirstChild());
/* 247:433 */     CodeStyleManager localCodeStyleManager = CodeStyleManager.getInstance(localProject);
/* 248:434 */     localCodeStyleManager.reformat(paramPsiMethod);
/* 249:435 */     return localPsiComment;
/* 250:    */   }
/* 251:    */   
/* 252:    */   public static boolean isTypeOfVoid(PsiType paramPsiType)
/* 253:    */   {
/* 254:446 */     return (paramPsiType != null) && (paramPsiType.equalsToText("void"));
/* 255:    */   }
/* 256:    */   
/* 257:    */   public static boolean isGetterMethod(PsiMethod paramPsiMethod)
/* 258:    */   {
/* 259:461 */     if (isTypeOfVoid(paramPsiMethod.getReturnType())) {
/* 260:462 */       return false;
/* 261:    */     }
/* 262:464 */     PsiParameterList localPsiParameterList = paramPsiMethod.getParameterList();
/* 263:465 */     if (localPsiParameterList.getParametersCount() != 0) {
/* 264:466 */       return false;
/* 265:    */     }
/* 266:468 */     return true;
/* 267:    */   }
/* 268:    */   
/* 269:    */   @Nullable
/* 270:    */   public static String getGetterFieldName(PsiMethod paramPsiMethod)
/* 271:    */   {
/* 272:490 */     if (!isGetterMethod(paramPsiMethod)) {
/* 273:491 */       return null;
/* 274:    */     }
/* 275:493 */     return PropertyUtil.getPropertyNameByGetter(paramPsiMethod);
/* 276:    */   }
/* 277:    */   
/* 278:    */   public static boolean isEnumField(PsiField paramPsiField)
/* 279:    */   {
/* 280:503 */     PsiType localPsiType = paramPsiField.getType();
/* 281:504 */     if (!(localPsiType instanceof PsiClassType)) {
/* 282:505 */       return false;
/* 283:    */     }
/* 284:507 */     PsiClassType localPsiClassType = (PsiClassType)localPsiType;
/* 285:508 */     PsiClass localPsiClass = localPsiClassType.resolve();
/* 286:509 */     return (localPsiClass != null) && (localPsiClass.isEnum());
/* 287:    */   }
/* 288:    */   
/* 289:    */   public static boolean isExceptionClass(PsiClass paramPsiClass)
/* 290:    */   {
/* 291:519 */     return InheritanceUtil.isInheritor(paramPsiClass, "java.lang.Throwable");
/* 292:    */   }
/* 293:    */   
/* 294:    */   @Nullable
/* 295:    */   public static PsiMethod findEqualsMethod(PsiClass paramPsiClass)
/* 296:    */   {
/* 297:530 */     PsiMethod[] arrayOfPsiMethod1 = paramPsiClass.findMethodsByName("equals", false);
/* 298:533 */     for (PsiMethod localPsiMethod : arrayOfPsiMethod1) {
/* 299:535 */       if (localPsiMethod.hasModifierProperty("public")) {
/* 300:540 */         if (!localPsiMethod.hasModifierProperty("static"))
/* 301:    */         {
/* 302:545 */           PsiType localPsiType = localPsiMethod.getReturnType();
/* 303:546 */           if (PsiType.BOOLEAN.equals(localPsiType))
/* 304:    */           {
/* 305:551 */             PsiParameter[] arrayOfPsiParameter = localPsiMethod.getParameterList().getParameters();
/* 306:552 */             if (arrayOfPsiParameter.length == 1) {
/* 307:557 */               if (arrayOfPsiParameter[0].getType().getCanonicalText().equals("java.lang.Object")) {
/* 308:562 */                 return localPsiMethod;
/* 309:    */               }
/* 310:    */             }
/* 311:    */           }
/* 312:    */         }
/* 313:    */       }
/* 314:    */     }
/* 315:566 */     return null;
/* 316:    */   }
/* 317:    */   
/* 318:    */   @Nullable
/* 319:    */   public static PsiMethod findHashCodeMethod(PsiClass paramPsiClass)
/* 320:    */   {
/* 321:577 */     PsiMethod[] arrayOfPsiMethod1 = paramPsiClass.findMethodsByName("hashCode", false);
/* 322:580 */     for (PsiMethod localPsiMethod : arrayOfPsiMethod1) {
/* 323:582 */       if (localPsiMethod.hasModifierProperty("public")) {
/* 324:587 */         if (!localPsiMethod.hasModifierProperty("static"))
/* 325:    */         {
/* 326:592 */           PsiType localPsiType = localPsiMethod.getReturnType();
/* 327:593 */           if (PsiType.INT.equals(localPsiType))
/* 328:    */           {
/* 329:598 */             PsiParameterList localPsiParameterList = localPsiMethod.getParameterList();
/* 330:599 */             if (localPsiParameterList.getParametersCount() == 0) {
/* 331:604 */               return localPsiMethod;
/* 332:    */             }
/* 333:    */           }
/* 334:    */         }
/* 335:    */       }
/* 336:    */     }
/* 337:608 */     return null;
/* 338:    */   }
/* 339:    */   
/* 340:    */   protected static boolean isTypeOf(PsiElementFactory paramPsiElementFactory, PsiType paramPsiType, String paramString)
/* 341:    */   {
/* 342:621 */     if (isTypeOfVoid(paramPsiType)) {
/* 343:622 */       return false;
/* 344:    */     }
/* 345:625 */     if (isPrimitiveType(paramPsiType)) {
/* 346:626 */       return false;
/* 347:    */     }
/* 348:629 */     GlobalSearchScope localGlobalSearchScope = paramPsiType.getResolveScope();
/* 349:630 */     if (localGlobalSearchScope == null) {
/* 350:631 */       return false;
/* 351:    */     }
/* 352:633 */     PsiClassType localPsiClassType = paramPsiElementFactory.createTypeByFQClassName(paramString, localGlobalSearchScope);
/* 353:634 */     return localPsiClassType.isAssignableFrom(paramPsiType);
/* 354:    */   }
/* 355:    */   
/* 356:    */   public static String[] getImplementsClassnames(PsiClass paramPsiClass)
/* 357:    */   {
/* 358:644 */     PsiClass[] arrayOfPsiClass = paramPsiClass.getInterfaces();
/* 359:646 */     if ((arrayOfPsiClass == null) || (arrayOfPsiClass.length == 0)) {
/* 360:647 */       return ArrayUtil.EMPTY_STRING_ARRAY;
/* 361:    */     }
/* 362:650 */     String[] arrayOfString = new String[arrayOfPsiClass.length];
/* 363:651 */     for (int i = 0; i < arrayOfPsiClass.length; i++)
/* 364:    */     {
/* 365:652 */       PsiClass localPsiClass = arrayOfPsiClass[i];
/* 366:653 */       arrayOfString[i] = localPsiClass.getName();
/* 367:    */     }
/* 368:656 */     return arrayOfString;
/* 369:    */   }
/* 370:    */   
/* 371:    */   public static boolean isPrimitiveType(PsiType paramPsiType)
/* 372:    */   {
/* 373:666 */     return paramPsiType instanceof PsiPrimitiveType;
/* 374:    */   }
/* 375:    */   
/* 376:    */   public static int getJavaVersion(PsiElement paramPsiElement)
/* 377:    */   {
/* 378:670 */     LanguageLevel localLanguageLevel = PsiUtil.getLanguageLevel(paramPsiElement);
/* 379:671 */     int i = 0;
/* 380:672 */     switch (1.$SwitchMap$com$intellij$pom$java$LanguageLevel[localLanguageLevel.ordinal()])
/* 381:    */     {
/* 382:    */     case 1: 
/* 383:674 */       i = 3;
/* 384:675 */       break;
/* 385:    */     case 2: 
/* 386:677 */       i = 4;
/* 387:678 */       break;
/* 388:    */     case 3: 
/* 389:680 */       i = 5;
/* 390:681 */       break;
/* 391:    */     case 4: 
/* 392:683 */       i = 6;
/* 393:684 */       break;
/* 394:    */     case 5: 
/* 395:686 */       i = 7;
/* 396:687 */       break;
/* 397:    */     case 6: 
/* 398:689 */       i = 8;
/* 399:    */     }
/* 400:692 */     return i;
/* 401:    */   }
/* 402:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.jetbrains.generate.tostring.psi.PsiAdapter

 * JD-Core Version:    0.7.0.1

 */